﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameworkManapro.BusinessModel;
using FrameworkManapro.Utilities;
using FrameworkManapro.WebUI;


namespace GMAC.ReportingSystem.Business.Components
{
    public class AgentComponent : IBusinessComponent<Entities.Agent>
    {
         private DataAccess.AgentDataAccess dac;

        public AgentComponent()
        {
            dac = new DataAccess.AgentDataAccess();
        }

        #region IBusinessComponent<Agent> Members

        public int Create(Entities.Agent entityToCreate)
        {
            if (Validate(entityToCreate))
            {
                var result = dac.Insert(entityToCreate);
                return result;
            }
            return 0;
        }

        public int Delete(Entities.Agent entityToDelete)
        {
            if (entityToDelete.ID <= 0)
                throw new ArgumentOutOfRangeException("AgentID", "Value must be greater than zero.");

            return dac.Delete(entityToDelete);
        }

        public int Edit(Entities.Agent entityToEdit)
        {
            if (entityToEdit.ID <= 0)
                throw new ArgumentOutOfRangeException("AgentID", "Value must be greater than zero.");

            return Validate(entityToEdit) ? dac.Update(entityToEdit) : 0;
        }

        public Entities.Agent GetDetails(object entityId)
        {
            return dac.Get(entityId);
        }

        protected bool Validate(Entities.Agent Agent)
        {
            if (Agent == null) throw new ArgumentNullException("Agent", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Agent.ID.ToString())) throw new ArgumentNullException("ID", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Agent.Status.ToString())) throw new ArgumentNullException("Status", "The argument cannot be null.");
            if (string.IsNullOrEmpty(Agent.ManagementGroup.ToString())) throw new ArgumentNullException("ManagementGroup", "The argument cannot be null.");


            return true;
        }

        #endregion

      
        public Entities.AgentCollection GetList()
        {
            return dac.GetList();
        }

        public Entities.AgentCollection Search(string UserName, string FirstName, string LastName, int? ManagementGroup, int? Turn, int? TypeAgent, PagingDetails paging)
        {
            if (UserName == null) UserName = string.Empty;
            if (FirstName == null) FirstName = string.Empty;
            if (LastName == null) LastName = string.Empty;
            return dac.Search(UserName, FirstName, LastName, ManagementGroup, Turn, TypeAgent, paging);
        }

    }
}
